Ext.define("Triends.Post",{
	extend: "Ext.data.Model",
	id: 'postlocationmodel',
	config: {
		idProperty: 'Number', //pullrefresh에서 중복된 값이 바인딩 되지 않도록 하기 위함!
		fields: ["Number", "ID", "WRITER", "TITLE", "CONTENT", "CONTINENT", "NATION", "DATE", "TIME", "HHMM"]
	}
});

var postLocationStore = Ext.create("Ext.data.Store",{
	model: "Triends.Post",
	id: 'postlocationstore',
	pageSize: 10,
	clearOnPageLoad: false,
//	proxy: {
//		type: "jsonp",
//		url: 'http://itscreater.cafe24.com/Triends/PostLocationList.jsp', // 속도 느릴 경우 proxy제거 
//		callbackkey: 'callback',
//		reader: {
//			type: "json",
//			root: "data"
//		}
//	},
	sorters: [
		{
			property: 'DATE',
			direction: 'DESC'
		},
		{
			property: 'TIME',
			direction: 'DESC'
		}
	]
});



Ext.define('Triends.view.PostLocationList', {
	extend: 'Ext.dataview.List',
	xtype: 'postlocationlist',
	id: 'postlocationlist',
	requires: ['Triends.view.LocationField1','Triends.view.SearchOverlay', 'Ext.plugin.ListPaging', 'Ext.plugin.PullRefresh'],
	config: {
		plugins: [
				{type: 'listpaging', autoPaging:true, clearOnPageLoad: false, loadMoreText: 'Next..'},
				{type: 'pullrefresh' /*refreshFn: function(plugin){postStore.load()}*/}
		],
		fullscreen: true,
		disableSelection: true,
		limit: 10,
		pressedCls: "x-item-selected",
		selectedCls: "",
		pressedDelay: 50,
		hidden: true,
		store: postLocationStore,
		itemTpl: "<b>{TITLE}</b><br><font size='2'>{WRITER}&nbsp;&nbsp;|&nbsp;&nbsp;{DATE}&nbsp;&nbsp;&nbsp;{HHMM}</font>",
		listeners: {
	        itemtap: function(list, index, target, model, e, eOpts) {
	        	
	        	
	        	try {
	        		var userid = Ext.getStore('UserStore').getAt(0).data.ID;
	        		if (model.data.ID == userid) {
	        		//Postinfo 삭제가능	
	        		var postdelbtn = Ext.getCmp('postlocationdeletebtn');
	        		var postmdfbtn = Ext.getCmp('postlocationmodifybtn');
	        		postdelbtn.setHidden(false);
	        		postmdfbtn.setHidden(false);
		        	} else {
		        		//Postinfo 삭제불가능
		        		var postdelbtn = Ext.getCmp('postlocationdeletebtn');
		        		var postmdfbtn = Ext.getCmp('postlocationmodifybtn');
		        		postdelbtn.setHidden(true);
		        		postmdfbtn.setHidden(true);
		        	}
	        	
		        	var postlocationtitlepanel1 = Ext.getCmp('postlocationtitlepanel1');
		        	var postlocationtitlepanel2 = Ext.getCmp('postlocationtitlepanel2');
		        	var postlocationcontentpanel = Ext.getCmp('postlocationcontentpanel');
		        	postlocationcontentpanel.setData(model.data);
		        	postlocationtitlepanel1.setData(model.data);
		        	postlocationtitlepanel2.setData(model.data);
		        	
		        	var PostLocationCommentStore = Ext.getStore('PostLocationCommentStore');
	 					PostLocationCommentStore.getProxy().setExtraParams({num: model.getData().Number});
	 					PostLocationCommentStore.load({
	 						callback: function() {
								Triends.app.getHistory().add(Ext.create('Ext.app.Action', {
            						url: 'mainpage/post/postinfolocation'
	        					}));		 	
	        				}
						}); 
	        	} catch(e){
	        		Ext.Msg.alert("","로그인이 필요합니다");
	        	}
	        }
	    },
		items: [
			{
			xtype: 'toolbar',
			docked: 'top',
			items: [
			{
				xtype: 'button',
				text: '지역검색',
				height: '20px',
				id: 'locationbutton',
				iconCls: 'maps',
				iconMask: true,
				handler: function(btn){				
					var locationOverlay = Ext.create("Triends.view.LocationOverlay");
					Ext.Viewport.add(locationOverlay);
				}
			},
			{
				xtype: 'spacer'
			},
			{
				xtype: 'button',
				id: 'locationsearchbutton',
				text: '검색',
				height: '20px',
				iconCls: 'search',
				iconMask: true,
				handler: function(btn){
					var searchoverlay = Ext.getCmp("searchoverlay");
					var searchaction = Ext.getCmp("searchaction");
					var postsearch = Ext.getCmp("postsearch");
					//로컬 스토리지 사용check
					var continent = localStorage.continent;
					var nation = localStorage.nation;
					// modal action 적용하기
					searchoverlay.showBy(btn);
					searchaction.addListener({
						tap: function(){
							var searchValue = Ext.getCmp("postsearch").getValue(); //listener밖에 두면 안된다!!!
							postLocationStore.removeAll();
							postLocationStore.setProxy({
								type: "jsonp",
								url: 'http://itscreater.cafe24.com/Triends/PostLocationSearch.jsp',
								extraParams: {searchvalue: searchValue, continent: continent, nation: nation},
								callbackkey: 'callback',
								pageParam: 'pages',
								limitParam: 'rpp',
								reader: {
									type: "json",
									rootProperty: "results",
									totalProperty: "total"
								}	
							})
							postLocationStore.loadPage(1);
//							Ext.data.JsonP.request({
//								url: 'http://itscreater.cafe24.com/Triends/PostLocationSearch.jsp',
//								params: {searchvalue: searchValue, continent: continent, nation: nation},
//								callbackkey: 'callback',
//								success: function(result){
//									postLocationStore.setData(result);
//									postsearch.reset();
//									searchoverlay.hide();
//								}
//							})
						}
					})
				}
			},
			{
				xtype: 'button',
				id: 'locationwritebutton',
				text: '글쓰기',
				height: '20px',
				iconCls: 'compose',
				iconMask: true,
				handler: function() {
					try{
						var userid = Ext.getStore("UserStore").getAt(0).data.ID;
			        	Triends.app.getHistory().add(Ext.create('Ext.app.Action', {
            				url: 'mainpage/post/postinglocation'
	        			}));	
					} catch(e){
						Ext.Msg.alert("","로그인이 필요합니다");
					}
				}
		}]
		}
		]
		
	}
});


    				
    				
    			